package client;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import client.abilities.CreatureAbility;

public class XMLReader {

	private String basePath = "src\\client\\resources\\";

	private NodeList init(String filename, String tagname) {
		try {
			File file = new File(filename);
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			return doc.getElementsByTagName(tagname);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String readCutscene(String filename) {
		String text = "";
		try {
			NodeList nodeLst = init(basePath + filename, "text");
			for (int s = 0; s < nodeLst.getLength(); s++) {
				Node fstNode = nodeLst.item(s);
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					Element fstElmnt = (Element) fstNode;

					NodeList roleElmntLst = fstElmnt
							.getElementsByTagName("statement");
					Element roleElmnt = (Element) roleElmntLst.item(0);
					NodeList role = roleElmnt.getChildNodes();
					text = role.item(0).getNodeValue();

				}
			}
			return text;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return text;
	}

	public HashMap<String, String> readCredits(String filename) {
		HashMap<String, String> hashmap = new HashMap<String, String>();
		try {
			NodeList nodeLst = init(basePath + filename, "person");
			for (int s = 0; s < nodeLst.getLength(); s++) {
				String key, value;
				Node fstNode = nodeLst.item(s);
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					Element fstElmnt = (Element) fstNode;

					NodeList roleElmntLst = fstElmnt
							.getElementsByTagName("role");
					Element roleElmnt = (Element) roleElmntLst.item(0);
					NodeList role = roleElmnt.getChildNodes();
					key = role.item(0).getNodeValue();

					NodeList nameElmntLst = fstElmnt
							.getElementsByTagName("name");
					Element nameElmnt = (Element) nameElmntLst.item(0);
					NodeList name = nameElmnt.getChildNodes();
					value = name.item(0).getNodeValue();
					hashmap.put(key, value);
				}
			}
			return hashmap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<String[]> readObject(String filename, String[] nodeNames,
			String baseNode) {
		String[] objectResults;
		ArrayList<String[]> array = new ArrayList<String[]>();
		try {
			NodeList nodeLst = init(basePath + filename, baseNode);
			for (int s = 0; s < nodeLst.getLength(); s++) {
				Node fstNode = nodeLst.item(s);
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					Element fstElmnt = (Element) fstNode;
					objectResults = new String[nodeNames.length];
					for (int n = 0; n < nodeNames.length; n++) {
						NodeList nodeList = fstElmnt
								.getElementsByTagName(nodeNames[n]);
						Element elem = (Element) nodeList.item(0);
						NodeList resultNode = elem.getChildNodes();
						objectResults[n] = resultNode.item(0).getNodeValue();
					}
					array.add(objectResults);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return array;
	}

	
}
